﻿namespace KingerRobot.MexicoSailunWMS.SpiService;

public class Wms2MesImpl : IWms2MesService
{
    /// <inheritdoc/>
    public int PushOutOrderStatus(List<PushOutOrderStatusParam> pushOutOrderStatusParams)
    {
        var result = 0;
        List<PushOutOrderStatusMesParam> pushOutOrderStatusMesParams = new();
        foreach (var pushOutOrderStatusParam in pushOutOrderStatusParams)
        {
            pushOutOrderStatusMesParams.Add(new()
            {
                OrderDetailId = pushOutOrderStatusParam.OrderDetailId,
                OrderStatus = pushOutOrderStatusParam.OrderStatus,
                TireBarcode = pushOutOrderStatusParam.TireBarcode
            });
        }
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushOutOrderStatus(pushOutOrderStatusMesParams);
        return result;
    }
    /// <inheritdoc/>
    public int PushWarehousing(string tireBarcode, string currentLoc)
    {
        var result = 0;
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushWarehousing(tireBarcode, currentLoc);
        return result;
    }
    /// <inheritdoc/>
    public int PushWarehousingOut(List<PushWarehousingOutParam> pushWarehousingOutParams)
    {
        var result = 0;
        List<PushWarehousingOutMesParam> pushWarehousingOutMesParams = new();
        foreach (var pushWarehousingOutParam in pushWarehousingOutParams)
        {
            pushWarehousingOutMesParams.Add(new()
            {
                OrderDetailId = pushWarehousingOutParam.OrderDetailId,
                TireBarcode = pushWarehousingOutParam.TireBarcode,
                CurrentLoc = pushWarehousingOutParam.CurrentLoc,
                TargetLoc = pushWarehousingOutParam.TargetLoc,
                EquipNo = pushWarehousingOutParam.EquipNo,
                EquipModel = pushWarehousingOutParam.EquipModel
            });
        }
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushWarehousingOut(pushWarehousingOutMesParams);
        return result;
    }
    /// <inheritdoc/>
    public int PushDirectDelivery(PushDirectDeliveryParam pushDirectDeliveryParam)
    {
        var result = 0;
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushDirectDelivery(new()
        //{
        //    BarCode = pushDirectDeliveryParam.BarCode,
        //    OrderDetailId = pushDirectDeliveryParam.OrderDetailId.ToString(),
        //    CurrentLoc = pushDirectDeliveryParam.CurrentLoc.ToString(),
        //    EquipNo = pushDirectDeliveryParam.EquipNo,
        //    EquipModel = pushDirectDeliveryParam.EquipModel,
        //});
        return result;
    }
    public int PushWarehousingFinish(string tireBarcode, string currentLoc)
    {
        var result = 0;
        try
        {
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushWarehousingFinish(tireBarcode, currentLoc);
        }
        catch (Exception ex)
        {
            WriteLog($"12.入库完成（WMS->MES）堆垛机入库任务完成后，由WMS将信息推送至MES 异常：{ex.Message}", true);
        }
        return result;
    }
    public int PushDirectDeliveryFinish(PushDirectDeliveryFinishParam pushDirectDeliveryFinishParam)
    {
        var result = 0;
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushDirectDeliveryFinish(new()
        //{
        //    BarCode = pushDirectDeliveryFinishParam.BarCode,
        //    OrderDetailId = pushDirectDeliveryFinishParam.OrderDetailId.ToString(),
        //    EquipNo = pushDirectDeliveryFinishParam.EquipNo,
        //    EquipModel = pushDirectDeliveryFinishParam.EquipModel,
        //});
        return result;
    }
    /// <inheritdoc/>
    public int PushWarehousingOutFinish(List<PushWarehousingOutFinishParam> pushWarehousingOutFinishParams)
    {
        var result = 0;
        List<PushWarehousingOutFinishMesParam> pushWarehousingOutFinishMesParams = new();
        foreach (var pushWarehousingOutFinishParam in pushWarehousingOutFinishParams)
        {
            pushWarehousingOutFinishMesParams.Add(new()
            {
                OrderDetailId = pushWarehousingOutFinishParam.OrderDetailId,
                TireBarcode = pushWarehousingOutFinishParam.TireBarcode,
                TargetLoc = pushWarehousingOutFinishParam.TargetLoc,
                EquipNo = pushWarehousingOutFinishParam.EquipNo,
                EquipModel = pushWarehousingOutFinishParam.EquipModel
            });
        }
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushWarehousingOutFinish(pushWarehousingOutFinishMesParams);
        return result;
    }
    /// <inheritdoc/>
    public int PushAbnormal(string barCode, long startLoc, long targetLoc)
    {
        var result = 0;
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        // result = mesService.PushAbnormal(barCode, startLoc, targetLoc);
        return result;
    }

    public ProductBean ProductGreenTire(string productCode)
    {
        ProductGreenTireParam? product = null;
        var mesService = K8.RSF.Client.Create<IMesWebService>();
        product = mesService.ProductGreenTire(productCode);
        if (product is null)
        {
            return null;
        }
        ProductBean productBean = new()
        {
            TireBarcode = product.TireBarcode,
            MaterialId = product.MaterialId,
            ProductEquip = product.ProductEquip,
            ProductTime = product.ProductTime,
            TireLock = product.TireLock,
            QuailtyStatus = product.QuailtyStatus,
            ProductBatch = product.ProductBatch,
        };
        return productBean;
    }

}
